<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 4] 4.16 Constant Expressions</TITLE>
<META NAME="author" CONTENT="Mark Grand">
<META NAME="date" CONTENT="Thu Jul 31 13:10:51 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="java">
<META NAME="title" CONTENT="Java Language Reference">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java Language Reference" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch04_15.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 4<br>Expressions</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch05_01.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JLR2-CH-4-SECT-16">4.16 Constant Expressions</A></h2>

<P CLASS=para>
A constant expression
is an expression that always produces the same result. More precisely,
a constant expression is an expression that produces a pure value
of a primitive data type and is only composed of the following:
<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>Literals of primitive data types

<P>
<li CLASS=listitem>String literals

<P>
<li CLASS=listitem>Variables that are declared <tt CLASS=literal>final</tt>
and are initialized by constant expressions

<P>
<li CLASS=listitem>Type casts to primitive data types or the type
<tt CLASS=literal>String</tt>

<P>
<li CLASS=listitem>The unary operators <tt CLASS=literal>+ -</tt>,
<tt CLASS=literal>~</tt>, and <tt CLASS=literal>!</tt>

<P>
<li CLASS=listitem>The binary operators *,
<tt CLASS=literal>/</tt>, <tt CLASS=literal>%</tt>, <tt CLASS=literal>+</tt>,
<tt CLASS=literal>-</tt>, <tt CLASS=literal>&lt;&lt;</tt>, <tt CLASS=literal>&gt;&gt;</tt>, <tt CLASS=literal>&gt;&gt;&gt;</tt>, <tt CLASS=literal>&lt;</tt>,
<tt CLASS=literal>&lt;=</tt>, &gt;=, <tt CLASS=literal>&gt;</tt>, ==,
<tt CLASS=literal>!=</tt>, <tt CLASS=literal>&amp;</tt>, <tt CLASS=literal>^</tt>,
<tt CLASS=literal>|</tt>, <tt CLASS=literal>&amp;&amp;</tt>, and
<tt CLASS=literal>||</tt>

<P>
<li CLASS=listitem>The ternary operator <tt CLASS=literal>?:</tt>

<P>
</UL>
<P CLASS=para>
Note that expressions that use <tt CLASS=literal>++</tt>,
<tt CLASS=literal>- -</tt>, and <tt CLASS=literal>instanceof</tt> are not
constant expressions. Also note that expressions that produce or
contain references to objects that are not <tt CLASS=literal>String</tt> objects
are never constant expressions.

<P CLASS=para>
The compiler generally evaluates a constant expression and substitutes
the result for the expression during the compilation process.

<P CLASS=para>
<b>References</b>
<A HREF="ch04_06.htm#JLR2-CH-4-SECT-6">Additive Operators</A>;
<A HREF="ch04_10.htm#JLR2-CH-4-SECT-10">Bitwise/Logical Operators</A>;
<A HREF="ch04_11.htm#JLR2-CH-4-SECT-11">Boolean Operators</A>;
<A HREF="ch04_04.htm#JLR2-CH-4-SECT-4.5">Casts</A>;
<A HREF="ch04_12.htm#JLR2-CH-4-SECT-12">Conditional Operator</A>;
<A HREF="ch04_09.htm#JLR2-CH-4-SECT-9">Equality Comparison Operators</A>;
<A HREF="ch05_05.htm#JLR2-CH-5-SECT-5.4.1">Interface Variables</A>;
<A HREF="ch06_01.htm#JLR2-CH-6-SECT-1.1">Local Variables</A>;
<A HREF="ch02_02.htm#JLR2-CH-2-SECT-2.3">Literals</A>;
<A HREF="ch04_05.htm#JLR2-CH-4-SECT-5">Multiplicative Operators</A>;
<A HREF="ch04_08.htm#JLR2-CH-4-SECT-8">Relational Comparison Operators</A>;
<A HREF="ch04_07.htm#JLR2-CH-4-SECT-7">Shift Operators</A>;
<A HREF="ch04_04.htm#JLR2-CH-4-SECT-4">Unary Operators</A>;
<A HREF="ch05_04.htm#JLR2-CH-5-SECT-4.4.1">Variables</A>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch04_15.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch05_01.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Data Type of an Expression</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Declarations</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
